package com.dark.sys.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dark.common.web.controller.BaseController;
import com.dark.common.web.model.DataGrid;
import com.dark.common.web.validate.AjaxResponse;
import com.dark.sys.service.UserService;
import com.dark.sys.web.dto.UserDto;

@Controller
@RequestMapping("/admin/sys/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Integer id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResponse add(UserDto dto) {
		this.userService.add(dto) ;
		return new AjaxResponse() ;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	public AjaxResponse delete(@PathVariable String id) {
		this.userService.delete(true, id) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public AjaxResponse deleteBatch(@RequestParam String ids, @RequestParam Boolean type) {
		this.userService.delete(type, ids) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteForPhysical", method=RequestMethod.POST)
	public AjaxResponse deleteForPhysical(@RequestParam String ids) {
		this.userService.deleteForPhysical(ids) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public AjaxResponse update(UserDto dto) {
		this.userService.update(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public UserDto get(@PathVariable Integer id, Model model) {
		return this.userService.get(id) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/datagrid")
	public DataGrid datagrid(UserDto dto) {
		return this.userService.datagrid(dto) ;
	}
	
	
	@RequestMapping(value="/ui_role/{ids}", method=RequestMethod.GET)
	public String ui_role(@PathVariable String ids, Model model) {
		if(null != ids) {
			model.addAttribute("ids", ids) ;
		}
		return viewName("ui_role") ;
	}
	
	
	@RequestMapping(value="/ui_change_pwd/{ids}", method=RequestMethod.GET)
	public String adminChangePwd(@PathVariable String ids,  Model model) {
		if(null != ids) {
			model.addAttribute("ids", ids) ;
		}
		return viewName("ui_change_pwd");
	}
	
	@ResponseBody
	@RequestMapping(value="/adminChangePassword")
	public AjaxResponse adminChangePassword(UserDto dto) {
		this.userService.adminChangePassword(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/changePassword")
	public AjaxResponse changePassword(UserDto dto) {
		return this.userService.changePassword(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/oldPasswordValidate", method=RequestMethod.POST)
	public Boolean oldPasswordValidate(@RequestParam Integer id, @RequestParam String oldPassword) {
		return this.userService.oldPasswordValidate(id, oldPassword) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/checkInfo")
	public Boolean checkInfo(UserDto dto) {
		return this.userService.checkInfo(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/getByUserName/{username}", method=RequestMethod.GET)
	public UserDto getByUserName(@PathVariable String username) {
		return this.userService.getByUsername(username) ;
	}
	
}
